package net.oschina.app.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import net.oschina.app.listener.OnRecycleViewItemClickListener;
import net.oschina.app.util.UIUtils;


/**
 * Created by U on 2017/3/8.
 */

public abstract class BaseRecyclerViewAdapter extends RecyclerView.Adapter {
    protected OnRecycleViewItemClickListener mItemListener;
    protected LayoutInflater mInflater;
    public BaseRecyclerViewAdapter(){
        mInflater= LayoutInflater.from(UIUtils.getContext());
    }

    // 设置item点击监听
    public void setOnItemClickListener(OnRecycleViewItemClickListener onItemListener){
        this.mItemListener=onItemListener;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        // 判断itemView之前是否设置过点击事件
        if(!holder.itemView.hasOnClickListeners()) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                        Toast.makeText(getActivity(), title, Toast.LENGTH_SHORT).show();
                    if(mItemListener!=null){
                        mItemListener.onClick(holder.itemView,holder.getLayoutPosition());
                    }
                }
            });
        }
    }

}
